﻿<html>
<head>
<link href="css/qam.css" rel="stylesheet" media="screen" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style>
</style>
</head>
<body>

	<div style="width:960px;margin:0 auto">
		<div id="message" ></div>
		
		<HR><h3>用户管理</h3>
		<div style="padding:10px;background:#E8E8FF;margin-top:10px;">
			<form method="post" action="#" name="myform" class="myform">
				<input type="hidden" value="" name="id" />
				<label><span>英文名：</span><input type="text" value="" name="name" /></label><BR />
				<label><span>中文名：</span><input type="text" value="" name="nick" /></label><BR />
				<label><span>密码：</span><input type="text" value="" name="passwd" /></label><BR />
				<label><span>用户组</span><span id="group_box"></span></label><BR />
				<input type="button" value="添加" onclick="add();" name="button"  />
			</form>
		</div>
		
		<form name="search_form" class="search_form">
			<label>用户<input type="text" value="" name="name" /></label>
			<label>昵称<input type="text" value="" name="nick" /></label>
			<label>用户组<span id="search_group_box"></span></label>
			<label><input type="button" value="搜索" name="search_btn" onclick="search(0)" /></label><BR><BR>
			<label>模糊查找<input type="text" value="" name="search_data" /></label>(关键字用空格分开)
			<label><input type="button" value="搜索" name="search_btn" onclick="search(1)" /><input type="button" value="重置" name="search_btn" onclick="search_clear()" /></label>
			
		</form>
		<div style="padding:10px;background:#E8E8FF;margin-top:10px;" id="listbox"></div>
		

	</div>



</body>

<script src="js/ajax.js" type="text/javascript"></script>
<script src="js/md5.js" type="text/javascript"></script>
<script src="js/function.js" type="text/javascript"></script>
<script>



function init(){load_group_list();}



var group_data;
/*加载group列表start*/
function load_group_list(){
	var _url=cgipath+"group-showlist";
	var ajax_obj=new AJAX_OBJ(_url,handle_load_group_list,handle_error,5000);
	ajax_obj.postData();
}

function handle_load_group_list(xmlHttp){
	eval("var ret_json="+xmlHttp.responseText);
	var str="<select name=\"usergroup\">";
	group_data=ret_json.listdata;
	for(var i=0;i<group_data.length;i++){
		str+="<option value=\""+group_data[i].id+"\" >"+group_data[i].name+"</option>";
	}
	str+="</select>";
	$$("group_box").innerHTML=str
	handle_load_group_list_search(xmlHttp);
}

function handle_load_group_list_search(xmlHttp){
	eval("var ret_json="+xmlHttp.responseText);
	var str="<select name=\"usergroup\">";
	group_data=ret_json.listdata;
	str+="<option value=\"\">请选择</option>";
	for(var i=0;i<group_data.length;i++){
		str+="<option value=\""+group_data[i].id+"\" >"+group_data[i].name+"</option>";
	}
	str+="</select>";
	$$("search_group_box").innerHTML=str;
	showlist();
}
/*加载group列表end*/


/*****user add********/
function add(){
	var _url=cgipath+"user-add";
	var ajax_obj=new AJAX_OBJ(_url,handle_add,handle_error,5000);
	var passwd=document.myform.passwd;
	var passwd_temp=passwd.value;
	if(passwd_temp.length>0)passwd.value=hex_md5(passwd_temp);//md5加密
	ajax_obj.postData(formToString(document.myform));
	passwd.value=passwd_temp;
}
function handle_add(xmlHttp){
	//$$("message").innerHTML=xmlHttp.responseText;return;
	eval("var ret_json="+xmlHttp.responseText);
	$$("message").innerHTML="【ret】"+ret_json.ret+"【msg】"+ret_json.msg;
	showlist();
}
/*****user getinfo********/
function edit(t){
	var _url=cgipath+"user-getinfo";
	var ajax_obj=new AJAX_OBJ(_url,handle_edit,handle_error,5000);
	ajax_obj.postData("&id="+t);
}
function handle_edit(xmlHttp){
	//$$("message").innerHTML=xmlHttp.responseText;return;
	eval("var ret_json="+xmlHttp.responseText);
	$$("message").innerHTML="【ret】"+ret_json.ret+"【msg】"+ret_json.msg;
	document.myform.id.value=ret_json.record.id;
	document.myform.name.value=ret_json.record.name;
	document.myform.nick.value=ret_json.record.nick;
	
	var str="<select name=\"usergroup\">";
	for(var i=0;i<group_data.length;i++){
		str+="<option value=\""+group_data[i].id+"\" ";
		if(ret_json.record.usergroup==1*group_data[i].id)str+=" selected=\"selected\" ";
		str+=">"+group_data[i].name+"</option>";
	}
	str+="</select>";
	$$("group_box").innerHTML=str
	
	
	document.myform.button.value="修改";
	document.myform.button.onclick=Function("update("+ret_json.record.id+")");
}

/*****user update********/
function update(){
	var _url=cgipath+"user-update";
	var ajax_obj=new AJAX_OBJ(_url,handle_update,handle_error,5000);
	var passwd=document.myform.passwd;
	var passwd_temp=passwd.value;
	if(passwd_temp.length>0)passwd.value=hex_md5(passwd_temp);//md5加密
	ajax_obj.postData(formToString(document.myform));
	passwd.value=passwd_temp;
}
function handle_update(xmlHttp){
	//$$("message").innerHTML=xmlHttp.responseText;return;
	eval("var ret_json="+xmlHttp.responseText);
	$$("message").innerHTML="【ret】"+ret_json.ret+"【msg】"+ret_json.msg;
	showlist();
}


/*****user del********/

function del(t){
	var _url=cgipath+"user-del";
	var ajax_obj=new AJAX_OBJ(_url,handle_del,handle_error,5000);
	ajax_obj.postData("&id="+t);
}
function handle_del(xmlHttp){
	//$$("message").innerHTML=xmlHttp.responseText;return;
	eval("var ret_json="+xmlHttp.responseText);
	$$("message").innerHTML="【ret】"+ret_json.ret+"【msg】"+ret_json.msg;
	showlist();
}

/*****user list********/
var page=1;//第1页
var reqnum=10;//每页显示10条
var order="id";//排序字段
var desc="1";//反序,0或空正常排序，1反序
var page_nums=0;

/*搜索*/
var search_str="";
function search(t){
	if(t==0){
		search_str="&search_flag=0";
		search_str+=formToString(document.search_form);
	}
	else {
		search_str="&search_flag=1";
		search_str+="&search_data="+encodeURIComponent(document.search_form.search_data.value);
	}
	showlist();
}

function search_clear(){
	formReset(document.search_form);
	search_str="";
	showlist();
}

function showlist(){
	//alert(search_str);
	var _url=cgipath+"user-showlist";
	var ajax_obj=new AJAX_OBJ(_url,handle_showlist,handle_error,5000);
	ajax_obj.postData(search_str+"&page="+page+"&reqnum="+reqnum+"&order="+order+"&desc="+desc);
}

function handle_showlist(xmlHttp){
	//$$("message").innerHTML=xmlHttp.responseText;return;
	eval("var ret_json="+xmlHttp.responseText);
	var str="<table><thead><tr>";
	str+="<td><a href='javascript:order=\"id\";desc=bin_exchange(desc);list_page(0)'>序号</a></td>";
	str+="<td><a href='javascript:order=\"name\";desc=bin_exchange(desc);list_page(0)'>用户</a></td>";
	str+="<td><a href='javascript:order=\"nick\";desc=bin_exchange(desc);list_page(0)'>昵称</a></td>";
	str+="<td><a href='javascript:order=\"usergroup\";desc=bin_exchange(desc);list_page(0)'>用户组</a></td>";
	str+="<td>操作</td></tr></thead>";
	page_nums=Math.ceil(ret_json.nums/reqnum);
	for(var i=0;i<ret_json.listdata.length;i++){
		str+="<tr><td>"+((page-1)*reqnum+i+1)+"</td>";
		str+="<td>"+ret_json.listdata[i].name+"</td>";
		str+="<td>"+ret_json.listdata[i].nick+"</td>";
		var usergroup_str="";
		for(var j=0;j<group_data.length;j++){
			if(group_data[j].id==ret_json.listdata[i].usergroup)usergroup_str=group_data[j].name;
		}
		str+="<td>"+usergroup_str+"</td>";
		str+="<td><a href='#' onclick='edit("+ret_json.listdata[i].id+")'>修改</a><a href='#' onclick='del("+ret_json.listdata[i].id+")'>删除</a></td></tr>";
	}
	str+="<tr><td colspan=4>共"+ret_json.nums+"条，每页"+reqnum+"条，"+page+"/"+page_nums+"</td>";
	str+="<td><a href='#' onclick='list_page(-1)'>上一页</a><a href='#' onclick='list_page(1)'>下一页</a></td></tr>";
	str+="</table>";
	$$("listbox").innerHTML=str
}

/*列表翻页*/
function list_page(t){
	page=page-(-1)*t;
	if(page<=0)page=1;
	else if(page>page_nums) page=page_nums;
	showlist();
}

</script>
<script src="js/public.js" type="text/javascript"></script>


</html>